/*
 * VFS operations relating to pathname translation
 */

#include <types.h>
#include <kern/errno.h>
#include <kern/limits.h>
#include <lib.h>
#include <synch.h>
#include <vfs.h>
#include <vnode.h>
#include <fs.h>

static struct vnode *bootfs_vnode = NULL;
static struct lock *bootfs_lock = NULL;

void
vfs_initbootfs(void)
{
	bootfs_lock = lock_create("bootfs_lock");
	if (bootfs_lock == NULL) {
		panic("vfs: Could not create bootfs lock\n");
	}
}

/*
 * Helper function for actually changing bootfs_vnode.
 */
static
void
change_bootfs(struct vnode *newguy)
{
	struct vnode *oldguy;

	lock_acquire(bootfs_lock);
	oldguy = bootfs_vnode;
	bootfs_vnode = newguy;
	lock_release(bootfs_lock);

	/* Do this without holding the lock so as to avoid deadlock */
	if (oldguy != NULL) {
		VOP_DECREF(oldguy);
	}
}

/*
 * Set bootfs_vnode.
 *
 * Bootfs_vnode is the vnode used for beginning path translation of
 * pathnames starting with /.
 *
 * It is also incidentally the system's first current directory.
 */
int
vfs_setbootfs(const char *fsname)
{
	char tmp[NAME_MAX+1];
	char *s;
	int result;
	struct vnode *newguy;

	snprintf(tmp, sizeof(tmp)-1, "%s", fsname);
	s = strchr(tmp, ':');
	if (s) {
		/* If there's a colon, it must be at the end */
		if (strlen(s)>0) {
			return EINVAL;
		}
	}
	else {
		strcat(tmp, ":");
	}

	result = vfs_chdir(tmp);
	if (result) {
		return result;
	}

	result = vfs_getcurdir(&newguy);
	if (result) {
		return result;
	}

	change_bootfs(newguy);

	return 0;
}

/*
 * Clear the bootfs vnode (preparatory to system shutdown).
 */
void
vfs_clearbootfs(void)
{
	change_bootfs(NULL);
}


/*
 * Common code to pull the device name, if any, off the front of a
 * path and choose the vnode to begin the name lookup relative to.
 */

static
int
getdevice(char *path, char **subpath, struct vnode **startvn)
{
	int slash=-1, colon=-1, i;
	struct vnode *vn;
	int result;

	/*
	 * Locate the first colon or slash.
	 */

	for (i=0; path[i]; i++) {
		if (path[i]==':') {
			colon = i;
			break;
		}
		if (path[i]=='/') {
			slash = i;
			break;
		}
	}

	if (colon < 0 && slash != 0) {
		/*
		 * No colon before a slash, so no device name
		 * specified, and the slash isn't leading or is also
		 * absent, so this is a relative path or just a bare
		 * filename. Start from the current directory, and
		 * use the whole thing as the subpath.
		 */
		*subpath = path;
		return vfs_getcurdir(startvn);
	}

	if (colon>0) {
		/* device:path - get root of device's filesystem */
		path[colon]=0;
		while (path[colon+1]=='/') {
			/* device:/path - skip slash, treat as device:path */
			colon++;
		}
		*subpath = &path[colon+1];
		
		result = vfs_getroot(path, startvn);
		if (result) {
			return result;
		}

		return 0;
	}

	/*
	 * We have either /path or :path.
	 *
	 * /path is a path relative to the root of the "boot filesystem".
	 * :path is a path relative to the root of the current filesystem.
	 */
	assert(colon==0 || slash==0);

	if (path[0]=='/') {
		lock_acquire(bootfs_lock);
		if (bootfs_vnode==NULL) {
			lock_release(bootfs_lock);
			return ENOENT;
		}
		VOP_INCREF(bootfs_vnode);
		*startvn = bootfs_vnode;
		lock_release(bootfs_lock);
	}
	else {
		assert(path[0]==':');

		result = vfs_getcurdir(&vn);
		if (result) {
			return result;
		}

		/*
		 * The current directory may not be a device, so it
		 * must have a fs.
		 */
		assert(vn->vn_fs!=NULL);

		*startvn = FSOP_GETROOT(vn->vn_fs);

		VOP_DECREF(vn);
	}

	while (path[1]=='/') {
		/* ///... or :/... */
		path++;
	}

	*subpath = path+1;

	return 0;
}

/*
 * Name-to-vnode translation.
 * (In BSD, both of these are subsumed by namei().)
 */

int
vfs_lookparent(char *path, struct vnode **retval,
	       char *buf, size_t buflen)
{
	struct vnode *startvn;
	int result;

	result = getdevice(path, &path, &startvn);
	if (result) {
		return result;
	}

	if (strlen(path)==0) {
		/*
		 * It does not make sense to use just a device name in
		 * a context where "lookparent" is the desired
		 * operation.
		 */
		result = EINVAL;
	}
	else {
		result = VOP_LOOKPARENT(startvn, path, retval, buf, buflen);
	}

	VOP_DECREF(startvn);
	return result;
}

int
vfs_lookup(char *path, struct vnode **retval)
{
	struct vnode *startvn;
	int result;

	result = getdevice(path, &path, &startvn);
	if (result) {
		return result;
	}

	if (strlen(path)==0) {
		*retval = startvn;
		return 0;
	}

	result = VOP_LOOKUP(startvn, path, retval);

	VOP_DECREF(startvn);
	return result;
}
